/**
 * 
 * 
 * 사용 불가 클래
 * 
 * 
**/
package classes.view.component
{
	import classes.controller.EClick;
	import classes.controller.ENumber;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class PopupPaging extends Sprite
	{
		private var _prevBtn:MovieClip;
		private var _nextBtn:MovieClip;
		private var _paging:Sprite;
		private var _pageArr:Array;
		private var _beforePage:int;
		
		public function PopupPaging()
		{
			super();
			setLayOut();
			init();	
		}
		
		private function setLayOut():void
		{
			this._pageArr	= [];

			this._prevBtn 	= this.getChildByName('prev_mc')	as MovieClip;
			this._nextBtn 	= this.getChildByName('next_mc')	as MovieClip;
			this._paging	= new Sprite();
			this.addChild(this._paging);
		}
		
		private function init():void
		{
			_prevBtn.buttonMode = true;
			_nextBtn.buttonMode = true;
			_prevBtn.addEventListener(MouseEvent.CLICK,		onClickHandler);
			_nextBtn.addEventListener(MouseEvent.CLICK,		onClickHandler);
		}
		private function onClickHandler(e:MouseEvent):void
		{
			switch(e.currentTarget){
				case _prevBtn :
					//trace('prev');
					dispatchEvent(new ENumber(_beforePage-1, ENumber.EVENT_NUMBER));
				break;
				case _nextBtn :
					//trace('next');
					dispatchEvent(new ENumber(_beforePage+1, ENumber.EVENT_NUMBER));
				break;
			}
		}
		public function pageOn($pageNum:int, $totalNum:int):void
		{
			_beforePage = $pageNum;
			
			resetPaging(); //paging reset
			pagingSetup($pageNum, $totalNum);
			setPageLayOut($pageNum, $totalNum);//paging.
		}
		private function setPageLayOut($pageNum:int, $totalNum:int):void
		{
			var i:int = 1 ;
			if($totalNum < 9){ //토탈이 9 미만일 경우.
				for(i = 1 ; i <= $totalNum ; i++){
					this._pageArr[i-1] 				= new PopupPagingNum();
					this._pageArr[i-1].pagingNum 	= i;
					this._pageArr[i-1].x			= 19*(i-1);
					this._paging.addChild(this._pageArr[i-1]);
					this._pageArr[i-1].addEventListener(MouseEvent.CLICK,	onClickPaging);
				}
				this._paging.x = ((19*9)-(19*$totalNum))/2;
				this._pageArr[$pageNum-1].selectPage 		= true;
			}else{
				if($pageNum < 5){
					for(i = 1 ; i < 10 ; i++){
						this._pageArr[i-1] 				= new PopupPagingNum();
						this._pageArr[i-1].pagingNum 	= i;
						this._pageArr[i-1].x			= 19*(i-1);
						this._paging.addChild(this._pageArr[i-1]);
						this._pageArr[i-1].addEventListener(MouseEvent.CLICK,	onClickPaging);
					}
					this._pageArr[$pageNum-1].selectPage 		= true;
					
				}else if($pageNum <= $totalNum-4){
					
					for(i = 1 ; i < 10 ; i++){
						this._pageArr[i-1] 				= new PopupPagingNum();
						this._pageArr[i-1].pagingNum 	= $pageNum+((i-1)-4);
						this._pageArr[i-1].x			= 19*(i-1);
						this._paging.addChild(this._pageArr[i-1]);
						this._pageArr[i-1].addEventListener(MouseEvent.CLICK,	onClickPaging);
					}
					this._pageArr[4].selectPage 		= true;

				}else{
					
					for(i = 1 ; i < 10 ; i++){
						this._pageArr[i-1] 				= new PopupPagingNum();
						this._pageArr[i-1].pagingNum 	= $totalNum + ((i-1)-8);
						this._pageArr[i-1].x			= 19*(i-1);
						this._paging.addChild(this._pageArr[i-1]);
						this._pageArr[i-1].addEventListener(MouseEvent.CLICK,	onClickPaging);
					}
					var arr:Array = [4,3,2,1];
					this._pageArr[4+arr[($totalNum-$pageNum)]].selectPage 		= true;
				}
			}
		}
		
		private function pagingSetup($pageNum:int, $totalNum:int):void
		{
			if($pageNum == $totalNum){
				_nextBtn.alpha = 0.3;
				_nextBtn.mouseEnabled  = false;
				_nextBtn.mouseChildren = false;
			}else{
				_nextBtn.alpha = 1;
				_nextBtn.mouseEnabled  = true;
				_nextBtn.mouseChildren = true;
			}
			if($pageNum == 1){
				_prevBtn.alpha = 0.3;
				_prevBtn.mouseEnabled  = false;
				_prevBtn.mouseChildren = false;
			}else{
				_prevBtn.alpha = 1;
				_prevBtn.mouseEnabled  = true;
				_prevBtn.mouseChildren = true;
			}
				_nextBtn.visible = true;
				_prevBtn.visible = true;
			
			if ($totalNum < 9) {
				_nextBtn.visible = false;
				_prevBtn.visible = false;
			}
		}
		
		private function resetPaging():void
		{
			if(_pageArr.length > 0){
				for (var i:int = 0 ; i < _pageArr.length ; i++) {
					if (this._pageArr[i] !== null && this._pageArr[i] !== undefined) {
						this._paging.removeChild(this._pageArr[i]);
					}
					this._pageArr[i] = null;
				}
				_pageArr.splice(0, _pageArr.length-1);
			}
		}
		
		
		private function onClickPaging(e:MouseEvent):void
		{
			dispatchEvent(new ENumber(e.currentTarget.pagingNum, ENumber.EVENT_NUMBER));
		}
		
		
	}
}